#
# AuthBase.pm
#
# Copyright (C) 2010 Towzer.com, LLC all rights reserved
#
#

package LsL::AuthBase;

use strict;
use Digest::SHA1;

my $secret = 'adsfjweovnxo23950#%*@0jfsm';

sub login {
  my ($username, $password, $ccode, $magic, $digest) = @_;

  my $userid = LsL::Auth::user_login({'user'   => $username,
                                      'passwd' => $password,
                                      'ccode'  => $ccode,
                                      'digest' => $digest,
                                      'magic'  => $magic,
                                     });
                                        
  if ($userid) {
    my $session_key = LsL::Auth::gen_key({'userid' => $userid,
                                          'secret' => $secret,});

    return undef unless $session_key;

    return undef unless
      LsL::Auth::new_session( {'session_key' => $session_key,
                               'userid'      => $userid} );
    return $session_key;
  }
}

sub check_session_key {
  my ($session_string, $in) = @_;

  # validate session (timeout, munge check, su, etc)
  my ($userid, $su_userid, $entity_id, $cis_entity_id, $cis_entity_type) = LsL::Auth::check_key($session_string, $secret, $in);

  return undef unless $userid;
  
  # set user
  return undef unless LsL::User::set_user( $session_string, $userid, $entity_id, $cis_entity_id, $cis_entity_type );

  return $session_string;
}
1;
